package com.ziyear.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ziyear.ssm.bean.Customer;
import com.ziyear.ssm.bean.QueryVo;
import com.ziyear.ssm.dao.CustomerDao;
import com.ziyear.ssm.service.CustomerService;
import com.ziyear.ssm.utils.Page;

@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDao customerDao;
	
	@Override
	public Page<Customer> getCustomerPage(QueryVo queryVo) {
		//设置起始坐标
		if(queryVo.getPage() != null) {
			if(queryVo.getPage() != 0) {
				queryVo.setStart((queryVo.getPage() - 1) * queryVo.getSize());
			}else {
				queryVo.setStart(0);
			}
		}
		//获取当前分页数据
		List<Customer> list = customerDao.getCustomerList(queryVo);
		//创建Page对象
		Page<Customer> page = new Page<>();
		page.setRows(list);
		//获取总记录数
		Integer count = customerDao.getCustomerCount(queryVo);
		page.setTotal(count);
		page.setSize(queryVo.getSize());
		page.setPage(queryVo.getPage());
		return page;
	}

	@Override
	public Customer getCustomerById(Long id) {
		Customer customer = customerDao.getCustomerById(id);
		return customer;
	}

	@Override
	public void updateCustomer(Customer customer) {
		customerDao.updateCustomer(customer);
		
	}

	@Override
	public void deleteCustomerById(Long id) {
		
		customerDao.deleteCustomerById(id);
	}

	@Override
	public void batchDelete(String ids) {
		for (String id : ids.split(",")) {
			customerDao.deleteCustomerById(Long.parseLong(id));
		}
		
	}

}
